package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.OrderDetailBean;
import com.yanchangapp.ui.view.OrderEnsureAtView;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-08-14.
 * description: 订单确认p层
 */
public class OrderEnsureAtPresenter extends BasePresenter<OrderEnsureAtView> {

    public OrderEnsureAtPresenter(BaseActivity context) {
        super(context);
    }

    public void queryOrderDetail(String orderId){
        HeadApiRetrofit.getInstance().getPurchaseOrderDetail(orderId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<OrderDetailBean>>() {
                    @Override
                    public void onNext(BaseResponse<OrderDetailBean> orderDetailBeanBaseResponse) {
                        if (orderDetailBeanBaseResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (null != getView()){
                                getView().getOrderDetailSuccess(orderDetailBeanBaseResponse.getData());
                            }
                        }else {
                            if (null != getView()){
                                getView().getOrderDetailFailure(orderDetailBeanBaseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (null != getView()){
                            getView().getOrderDetailFailure(exception.getErrorInfo());
                        }
                    }
                });
    }

    /**
     * 作废订单
     */
    public void dropOrderByID(String orderId){
        HeadApiRetrofit.getInstance().deleteOrderById(orderId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                            if (getView() != null){
                                getView().dropOrderSuccess();
                            }
                        }else {
                            if (getView() != null){
                                getView().dropOrderFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().dropOrderFailure(exception.getErrorInfo());
                        }
                    }
                });
    }

    /**
     * 提交订单
     * @param orderId
     */
    public void submitOrderByID(String orderId){
        HeadApiRetrofit.getInstance().submitApprovalOrderByID(orderId)
            .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                            if (getView() != null){
                                getView().submitOrderSuccess();
                            }
                        }else {
                            if (getView() != null){
                                getView().submitOrderFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().dropOrderFailure(exception.getErrorInfo());
                        }
                    }
                });
    }
}
